/* @(#)GSBargainModifyAction.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-4-26
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.upload.FormFile;

import com.hitrust.gs.bean.GSBargainBean;
import com.hitrust.gs.bean.GSBargainChargeBean;
import com.hitrust.gs.bean.GSBargainModfiyBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSBargainProcessor;
import com.hitrust.gs.dao.GSBargainDAO;
import com.hitrust.gs.form.GSBargainAddForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.GenRandNum;
import com.hitrust.gs.util.exception.GSException;

/**
 * 修改合同信息
 */
public class GSBargainModifyAction extends GSBaseAction{

    
    public String execute(GSBaseForm aForm) throws Exception {
        GSBargainAddForm tForm = (GSBargainAddForm)aForm;
        
        GSBargainBean tGSBargainBean=new GSBargainBean();
        
        String tDelfile=aForm.getRequest().getParameter("delfile");
        
        GSBargainModfiyBean tGSBargainModfiyBean=new GSBargainModfiyBean();
        
        //获得操作员号
        GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        //获取系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String buf = df.format(new java.util.Date(System.currentTimeMillis()));
        
        try{
            BeanUtils.copyProperties(tGSBargainBean,tForm);
            if(tDelfile.equals("new")){
                String tPath=GSSystemConfigurator.getParam("gs.upload.savepath");
                //获得新的文件
                FormFile tNewFile = tForm.getManufactureimage();
                if(!(tNewFile.getFileSize()==0)){
                    String tSaveName = tNewFile.getFileName();
//                  文件系统时间
                    SimpleDateFormat df1 = new SimpleDateFormat("MMddHHmmss");
                    String buf1 = df1.format(new java.util.Date(System.currentTimeMillis()));
                    
                    String tSaveFileName=tSaveName.substring(0,tSaveName.indexOf("."));
                    String tFileType=tSaveName.substring(tSaveName.indexOf("."));
                    String tFileName=tSaveFileName+buf1+tFileType;
                    
                    GSBargainProcessor.saveFile(tNewFile,tPath,tFileName);
                    tGSBargainBean.setGs_bgn_filename(tFileName);
                }
            }
            
            if(tDelfile.equals("2")){
                //String tPath=tForm.getSession().getServletContext().getRealPath("/upload");
                String tPath=GSSystemConfigurator.getParam("gs.upload.savepath");
                String filename=tGSBargainBean.getGs_bgn_filename();
                String realFilePath = tPath+"\\"+filename;
                File tOldFile = new File(realFilePath);
                //删除文件
                tOldFile.delete();
                //获得新的文件
                FormFile tNewFile = tForm.getManufactureimage();
                if(!(tNewFile.getFileSize()==0)){
                    String tSaveName = tNewFile.getFileName();
//                  文件系统时间
                    SimpleDateFormat df1 = new SimpleDateFormat("MMddHHmmss");
                    String buf1 = df1.format(new java.util.Date(System.currentTimeMillis()));
                    
                    String tSaveFileName=tSaveName.substring(0,tSaveName.indexOf("."));
                    String tFileType=tSaveName.substring(tSaveName.indexOf("."));
                    String tFileName=tSaveFileName+buf1+tFileType;
                    
                    GSBargainProcessor.saveFile(tNewFile,tPath,tFileName);
                    tGSBargainBean.setGs_bgn_filename(tFileName);
                }
                else{
                    tGSBargainBean.setGs_bgn_filename("");
                }
            }
            
            String tContext1="";
            String tContext2="";
            String tBgmContext="";
            FormFile tNewFile = tForm.getManufactureimage();
            //合同修改内容
            tContext1=GSBargainProcessor.addBargainModfiyContent(tGSBargainBean,tDelfile,tNewFile);
            
            //修改合同信息
            GSBargainProcessor.modifyBargainTask(tGSBargainBean,tForm.getGs_bgn_old_serial());
            
            //修改合同费用信息
            GSBargainChargeBean tGSBargainChargeBean=new GSBargainChargeBean();
           
            String[] tBgcType = tForm.getGs_bgc_type();
            String[] tBgcContent=tForm.getGs_bgc_content();
            String[] tBgcId=tForm.getGs_bgc_id();
            
            String[] tBgcTypeOld = tForm.getGs_bgc_type_old();
            String[] tBgcContentOld = tForm.getGs_bgc_content_old();
            
            int num=0;
            String tSuccess="";
            
            if(tBgcId!=null){
               num=tBgcId.length; 
               GSBargainDAO tGSBargainDAO=new GSBargainDAO();
             
               for(int i=0;i<tBgcId.length;i++){
                    tGSBargainChargeBean.setGs_bgc_id(tBgcId[i]);
                    tGSBargainChargeBean.setGs_bgc_bgn_id(tForm.getGs_bgn_id());
                    tGSBargainChargeBean.setGs_bgc_type(tBgcType[i]);
                    tGSBargainChargeBean.setGs_bgc_content(tBgcContent[i]);
                    
                    if(!tBgcType[i].equals(tBgcTypeOld[i])){
                        tSuccess="ok";
                      }
                    if(!tBgcContent[i].equals(tBgcContentOld[i])){
                        tSuccess="ok";
                      }
                    
                    if(tBgcContent[i].equals("")&&tBgcType[i].equals("")){
                        tGSBargainDAO.deleteBargainChargeID(tBgcId[i]);
                    }else{
                        GSBargainProcessor.modifyBargainChargeTask(tGSBargainChargeBean);
                    }
                 }
            }
            for(int j=num;j<tBgcType.length;j++) { 
              if(!(tBgcType[j].equals("")&&tBgcContent[j].equals(""))){
                    tGSBargainChargeBean.setGs_bgc_id(GenRandNum.genDTRandNum());
                    tGSBargainChargeBean.setGs_bgc_bgn_id(tForm.getGs_bgn_id());
                    tGSBargainChargeBean.setGs_bgc_type(tBgcType[j]);
                    tGSBargainChargeBean.setGs_bgc_content(tBgcContent[j]);
                    
                    GSBargainProcessor.addBargainChargeTask(tGSBargainChargeBean);
                    tSuccess="ok";
                }
              }
           
            if(tSuccess.equals("ok")){
                tContext2="修改费用信息 ";
            }
            if(tContext1.equals("")&&tContext2.equals("")){
                tBgmContext="修改合同部分";
            }
            else{
                tBgmContext=tContext1+tContext2;
            }
            
            //设置修改信息
            tGSBargainModfiyBean.setGs_bgm_serial(tGSBargainBean.getGs_bgn_id());
            tGSBargainModfiyBean.setGs_bgm_id(GenRandNum.genDTRandNum());
            tGSBargainModfiyBean.setGs_bgm_userid(tOperBean.getGs_oper_id());
            tGSBargainModfiyBean.setGs_bgm_date(buf);
            tGSBargainModfiyBean.setGs_bgm_context(tBgmContext);
            //添加修改信息
            GSBargainProcessor.addBargainModfiyTask(tGSBargainModfiyBean);
            }
            catch (GSException err) {
                if(err.getCode() != null ) { 
                    HashMap tMap = new HashMap();
                    tMap.put("err",err.getMessage());
                    aForm.getRequest().setAttribute("_error",tMap);
                    aForm.getRequest().setAttribute("exception_desc",err.getMessage());
                    aForm.getRequest().setAttribute("exception_code",err.getCode());
                    
                    aForm.getRequest().setAttribute("action", "A170201.do");
                    return GSBaseConstants.FORWARD_ERROR;
                } 
            }
            aForm.getRequest().setAttribute("message", "修改合同信息成功!");
            aForm.getRequest().setAttribute("backstep", "0");
            aForm.getRequest().setAttribute("requery","true");
            return GSBaseConstants.FORWARD_SUCCESS;
        }
}
